
//
// 110403 - AH - Created.
// 110413 - AH - Finished implementation.
//

package org.soekrat.engine.pitbul.parser;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.soekrat.engine.Soekrat2Engine;
import org.soekrat.engine.forest.Tree;
import org.soekrat.engine.pit.Content;
import org.soekrat.engine.pit.Node;
import org.soekrat.engine.pit.Pit;

/**
 * <p>
 *   {@link FormParser} for the <i>node</i> form.
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC)  
 */
public final class NodeFormParser extends FormParser 
{
  /**
   * <p>
   *   Constructor.
   * </p>
   */
  public NodeFormParser(){ super("node"); }
  
  private static enum State
  {
    START, NODE_NAME, CONTENT, IN, ONTOLOGY, END
  };

  @Override
  public void parse(Tree tree, Soekrat2Engine se) throws IOException 
  {
    if (tree==null)
    {
      throw new NullPointerException("tree");
    }
    if (se==null)
    {
      throw new NullPointerException("se");
    }
    
    Pit pit=se.getPit();
    
    PitbulTokens tokens=(PitbulTokens)tree.getAttribute("post-tokens");
    
    State state=State.START;
    
    Node node=null;
    
    int idx=1;
    
    while (true)
    {
      switch (state)
      {
        case START :
          getExpectedToken(tokens, idx++, "*be");
          state=State.NODE_NAME;
        break;
      
        case NODE_NAME :
          String name=getToken(tokens, idx++);
        
          node=new Node(name);
        
          if (tokens.passedEnd(idx)) 
          {
            state=State.END;
          }
          else                       
          {            
            state=State.CONTENT;
          }
          
        break;
      
        case CONTENT :
        {
          getExpectedToken(tokens, idx++, "content");
          
          String url=getToken(tokens, idx++);
          
          getExpectedToken(tokens, idx++, "mime-type");
          String mime=getToken(tokens, idx++);
        
          //Content content=new Content(url, mime);
          Content content=new Content(url);
          node.addContent(content);
        
          if (tokens.passedEnd(idx)) 
          {
            state=State.END;
          }
          else                       
          {
            List<String> ont=new ArrayList<String>();
            idx=scanOntologies(tokens, idx, ont);
            if (!ont.isEmpty()) content.addOntologies(ont);
            
            if (tokens.passedEnd(idx)) state=State.END;
            else                       state=State.CONTENT;
          }
        }
        break;
      
        case END :
          pit.addNode(node);
          
          System.out.println("PARSED NODE: "+node);
        return;
        
      }
    }
  }
  
  private final static String[] c_ontStart=new String[]{ "in", "ontology" };
  
  private final static String[] c_ontEnd=new String[]{ ";" };
  
  private int scanOntologies(PitbulTokens tokens, int idx, List<String> ont)
  { 
    if (tokens.match(idx, c_ontStart))
    {
      return tokens.getTokensTo(idx+2, ont, c_ontEnd);
    }
    else
    {
      return idx;
    }
  }
  
}
